package com.fita.entity.wallet;

import java.math.BigDecimal;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import com.demo2do.core.utils.DateFormatUtils;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fita.entity.order.Order;

/**
 * 分销奖励
 *
 * @description:
 * @author: 11200
 * @date: 2019/7/1
 */
@Entity
@Table(name = "distribution_reward_record")
public class DistributionRewardRecord {

    @Id
    @GeneratedValue
    private Long id;

    /**
     * 一级代理提成率
     */
    private Integer rate;

    /**
     * 奖励金额(分)
     */
    private Integer amount;

    /**
     * 当前余额（分）
     */
    private Integer currentUserWalletAmount;

    private Date createTime;

    @Enumerated(EnumType.STRING)
    private DistributionRewardRecordStatus status;

    /**
     * 一级代理钱包
     */
    @ManyToOne
    @JoinColumn(nullable = false)
    @JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private UserWallet targetUserWallet;

    @ManyToOne
    @JoinColumn(nullable = false)
    @JsonIdentityInfo(generator= ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Order order;

    @Column(name = "is_disabled")
    private Boolean disabled;

    /**
     *
     */
    public DistributionRewardRecord() {
    }

    public DistributionRewardRecord(Integer rate, Integer amount, UserWallet targetUserWallet, Order order) {
        this.rate = rate;
        this.amount = amount;
        this.createTime = new Date();
        this.status = DistributionRewardRecordStatus.UNCONSUMED;
        this.targetUserWallet = targetUserWallet;
        this.order = order;
        this.disabled = false;
    }

    /**
     * 获取格式化价格
     * @return
     */
    @Transient
    public BigDecimal getPrettyAmount(){
        if (amount == null){
            return BigDecimal.ZERO;
        }
        return new BigDecimal(amount).divide(new BigDecimal(100));
    }

    /**
     * 取消
     */
    public void cancel() {
        this.status = DistributionRewardRecordStatus.CANCEL;
    }


    /**
     * 核销
     */
    public void consume(Integer userWalletAmount) {

        this.status = DistributionRewardRecordStatus.CONSUMED;

        this.currentUserWalletAmount = userWalletAmount;

    }

    /**
     * 计算预计核销时间
     * @return
     */
    @Transient
    public Date getExpectedConsumedDate(){
        return this.order.getConsumedDate();
    }

    /**
     * 获取代理名
     * @return
     */
    @Transient
    public String getAgentName(){
        return this.order.getAgent().getName();
    }

    /**
     * 格式化日期
     * @return
     */
    @Transient
    public String getFormatedCreateTime(){
        return DateFormatUtils.format(this.createTime);
    }

    /**
     * 格式化日期
     * @return
     */
    @Transient
    public String getFormatedExpectedConsumedDate(){
        return DateFormatUtils.format(getExpectedConsumedDate());
    }

    /**
     * 获取状态描述
     * @return
     */
    @Transient
    public String getStatusDesc(){
        return this.status.getDescription();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public DistributionRewardRecordStatus getStatus() {
        return status;
    }

    public void setStatus(DistributionRewardRecordStatus status) {
        this.status = status;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Boolean getDisabled() {
        return disabled;
    }

    public void setDisabled(Boolean disabled) {
        this.disabled = disabled;
    }

    public UserWallet getTargetUserWallet() {
        return targetUserWallet;
    }

    public void setTargetUserWallet(UserWallet targetUserWallet) {
        this.targetUserWallet = targetUserWallet;
    }

    public Integer getRate() {
        return rate;
    }

    public void setRate(Integer rate) {
        this.rate = rate;
    }

    public Integer getCurrentUserWalletAmount() {
        return currentUserWalletAmount;
    }

    public void setCurrentUserWalletAmount(Integer currentUserWalletAmount) {
        this.currentUserWalletAmount = currentUserWalletAmount;
    }
}
